package com.itheima.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass =MenuService.class )
public class MenuServiceImp implements MenuService {
    @Autowired
    private MenuDao menuDao;

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
//        Page<Menu> page =menuDao.findByPage();
//        List<Menu> menuInfos = page.getResult();
//        for (Menu menuInfo : menuInfos) {
//            Integer id = menuInfo.getId();
//            //通过Id查询子Id
//        }

        Page<Map> menuInfos = menuDao.findByPage();
        for (Map menuInfo : menuInfos) {
            Integer id = (Integer)menuInfo.get("id");
            List<Map> childrens = menuDao.findChildrenById(id);
            if(childrens!=null){
                for (Map children : childrens) {
                    children.put("LEVEL","二级菜单");
                }
                menuInfo.put("children",childrens) ;
                menuInfo.put("LEVEL","一级菜单");
                menuInfo.put("linkUrl","无");
            }

        }

        return new PageResult(menuInfos.getTotal(),menuInfos.getResult());
    }

    @Override
    public Result findAllMenu() {
        List<Map> allMenu = menuDao.findAllMenu();
        List<Map> maps = new ArrayList<>();
            Map<String, Object> map = new HashMap<>();
            map.put("value","子菜单");
            map.put("label","子菜单");
            map.put("children",allMenu);
            Map<String, Object> map1 = new HashMap<>();
            map1.put("value","父菜单");
            map1.put("label","父菜单");
            maps.add(map);
            maps.add(map1);

        return new Result(true,null,maps);
    }

    @Override
    public Result add(Map menu) {
        String childred = (String) menu.get("childred");
        //父菜单
        if(childred==null){
            menu.put("level",1);
            int max = menuDao.findMaxPath();
            menu.put("path",++max);
            menu.put("priority",--max);
            menuDao.addFaMenu(menu);
        }else{
            String father =(String)menu.get("father");
            Integer id = menuDao.findIdByMenuName(father);
            menu.put("parentMenuId",id);
            menu.put("level",2);
            menuDao.add(menu);
        }

        return new Result(true,"新增菜单成功");
    }

    @Override
    public Map findById(Integer id) {
        List<Map> allMenu = menuDao.findAllMenu();
        List<Map> maps = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("value","子菜单");
        map.put("label","子菜单");
        map.put("children",allMenu);
        Map<String, Object> map1 = new HashMap<>();
        map1.put("value","父菜单");
        map1.put("label","父菜单");
        maps.add(map);
        maps.add(map1);
        Map map2 = menuDao.findNameById(id);
        map2.put("menus",maps);
        return map2;
    }

    @Override
    public Result edit( Map map) {
        String childred = (String) map.get("childred");
        String name = (String) map.get("name");
        Object id2 = map.get("id");
        String string = id2.toString();
        Integer id = Integer.parseInt(string);
        String description = (String)map.get("description");
        //父菜单
        if(childred==null){

            int max = menuDao.findMaxPath();
            HashMap<Object, Object> map2 = new HashMap<>();
            map2.put("name",name);
            System.out.println(name);
            map2.put("description",description);
            map2.put("path",++max);
            map2.put("priority",--max);
            map2.put("id",id);
            menuDao.editFaMenu( map2);
        }else{
            String father =(String)map.get("father");
            int id1 = menuDao.findIdByMenuName(father);
             Map mapInfo = new HashMap<>();
            mapInfo.put("name",name);
            System.out.println(name);
            mapInfo.put("parentMenuId",id);
            mapInfo.put("level",2);

            mapInfo.put("description",description);
            mapInfo.put("id",id);
            menuDao.edit( mapInfo);
        }

        return new Result(true,"编辑成功");
    }

    @Override
    public Result delete(int id) {
        Integer parentId = menuDao.findParentIdById(id);
        String linkUrl = menuDao.findlinkUrlById(id);
        List<Integer> childRensIdById = menuDao.findChildRensIdById(id);
        if(childRensIdById.size()==0&&linkUrl==null){
            menuDao.deleteMenuById(id);
            return new Result(true,"删除成功") ;
        }else{
            return new Result(false,"删除失败") ;
        }

    }

//    //根据Id查询单个菜单
//    @Override
//    public Map findById(Integer id) {
//        Menu menu = menuDao.findById(id);
//        return null;
//    }
}
